import {
  defineStore
} from 'pinia'
export const orderUserStore = defineStore('order', {
  state: () => {
    return {
     order:{
       list:[]
     },
     orderser:{
       list:[]
     },
     orderxinxi:{
       data:{}
     }
    }
  },
  getters: {
    //总价
     total() {
       const list = this.order.list
       return list.length
         ? list.reduce((acc, item) => ( acc + item.unit_price * item.quantity ), 0)
         : 0
     },
   //总数
    allcount() {
      const list = this.order.list
      return list.length
        ? list.reduce((acc, item) => ( acc + item.quantity) , 0)
        : 0
    },
   },
  actions: {
   //获取订单信息
   async getorder(data) {
    const response = await uni.$http.post("/order/getorder",data)
     this.order.list = response.data
  },
  //发送获取购物车信息请求
  async getcart(){
    const response = await uni.$http.get("/cart/getcart")
     this.order.list = response.data.filter((item)=>item.checked )
  },
  //发送提交订单
  async submitorder(adderssid,status){
    //定义订单信息
    const data = {
      "adderssid":adderssid,
      "proutlist":JSON.parse(JSON.stringify(this.order.list)),
      "status":status
    }
    // console.log(data);
    const response = await uni.$http.post("/order/submitorder",data)
  },
 //获取分类订单信息
 async getorderers(status){
   const response = await uni.$http.get("/order/getorderers",{"status":status})
   this.orderser.list = response.data
 },
 //继续付款
 async jixufukuan(id,statuser){
   const response = await uni.$http.put("/order/jixufukuan",{"id":id})
   this.getorderers(statuser)
   uni.showToast({
     title: response.data,
     duration: 2000,
     icon: "success"
   });
 },
 //获取订单信息
  async getorderxinxi(id) {
   const response = await uni.$http.get("/order/getorderxinxi",{"id":id})
    this.orderxinxi.data = response.data
 },
 //取消订单
 async mulaneyorder(id,statuser){
    const response = await uni.$http.put("/order/mulaneyorder",{"id":id})
    this.getorderers(statuser)
    uni.showToast({
      title: response.data,
      duration: 2000,
      icon: "success"
    });
 },
 //删除订单
 async deleteorder(id,statuser){
    const response = await uni.$http.put("/order/deleteorder",{"id":id})
    this.getorderers(statuser)
    uni.showToast({
      title: response.data,
      duration: 2000,
      icon: "success"
    });
 },
 //确认收货
 async querenshouhuo(id,statuser){
   const response = await uni.$http.put("/order/querenshouhuo",{"id":id})
   this.getorderers(statuser)
   uni.showToast({
     title: response.data,
     duration: 2000,
     icon: "success"
   });
 },
 //评价商品
 async evaluation(data){
   const response = await uni.$http.post("/user/evaluation",data)
   if(response.data.code!=200){
     uni.showToast({
       title: response.data.msg,
       duration: 2000,
       icon: "error"
     });
   }else{
     uni.showToast({
       title: response.data.msg,
       duration: 2000,
       icon: "success"
     });
     uni.navigateTo({
       url: `/subPack/goods_detail/goods_detail?goods_id=${data.orderid}`
     })
   }
 }, 
 }
})